import request from '@/utils/request'

export function getAppList(params) {
  return request({
    url: '/online/management/operator/app',
    method: 'get',
    params
  })
}

export function getAppPage(params) {
  return request({
    url: '/online/management/operator/app/apps',
    method: 'get',
    params
  })
}

export function getLastestApp(packageName) {
  return request({
    url: `/online/management/operator/app/${packageName}/lastest`,
    method: 'get'
  })
}

// 查询可推送的全部应用列表
export function getAppListForDevExport(params) {
  return request({
    url: '/online/management/operator/app/device/apps/list',
    method: 'get',
    params
  })
}

export function getDeviceAppPage(params) {
  return request({
    url: '/online/management/operator/app/device/apps',
    method: 'get',
    params
  })
}

export function getBatchAppPage(params) {
  return request({
    url: '/online/management/operator/app/batch/apps',
    method: 'get',
    params
  })
}

export function getAppVersionPage(params) {
  return request({
    url: '/online/management/operator/app/versions',
    method: 'get',
    params
  })
}

export function getAppVersionList(params) {
  return request({
    url: '/online/management/operator/app/versions/list',
    method: 'get',
    params
  })
}

export function getAppVersionInfo(appFileId) {
  return request({
    url: `/online/management/operator/app/appVersionDetail/${appFileId}`,
    method: 'get'
  })
}

export function appRelease(data) {
  return request({
    url: '/online/management/operator/app/releaseApk',
    method: 'post',
    timeout: 30 * 60 * 1000,
    data
  })
}

export function getDevSn(devSn) {
  return request({
    url: `/online/management/operator/app/${devSn}`,
    method: 'get'
  })
}

export function getCanaryDevSn(params) {
  return request({
    url: `/online/management/operator/app/grayscale/${params.pkgName}/${params.versionName}/devices`,
    method: 'get',
    params
  })
}

export function addCanaryDevSn(data) {
  return request({
    url: `/online/management/operator/app/grayscales/${data.appFileId}/bind/${data.devSn}`,
    method: 'post',
    data
  })
}

export function deleteCanaryDevSn(data) {
  return request({
    url: `/online/management/operator/app/grayscales/${data.grayscaleId}/unbind`,
    method: 'delete',
    data
  })
}

export function getAppDownloadInfo(appFileId) {
  return request({
    url: `/online/management/operator/app/${appFileId}/download`,
    method: 'get'
  })
}

export function appVersionDelete(appFileId) {
  return request({
    url: `/online/management/operator/app/${appFileId}`,
    method: 'delete'
  })
}

export function deleteAppVersionAndParam(data) {
  return request({
    url: `/online/management/operator/app/${data.appFileId}`,
    method: 'delete',
    data
  })
}

export function distribute(data) {
  return request({
    url: `/online/management/operator/app/distribute`,
    method: 'post',
    data
  })
}

export function queryDistributed(packageName) {
  return request({
    url: `/online/management/operator/app/${packageName}/distributed`,
    method: 'get'
  })
}

export function firmwareDistribute(data) {
  return request({
    url: `/online/management/operator/app/firmware/distribute`,
    method: 'post',
    data
  })
}

export function queryFirmwareDistributed(appFileId) {
  return request({
    url: `/online/management/operator/app/firmware/${appFileId}/distributed`,
    method: 'get'
  })
}

export function productionRelease(data) {
  return request({
    url: `/online/management/operator/app/canary/${data}/release`,
    method: 'post',
    data
  })
}

export function versionConfirm(packageName, versionCode) {
  return request({
    url: `/online/management/operator/app/${packageName}/${versionCode}/confirm`,
    method: 'get'
  })
}

export function applicationCheck(packageName) {
  return request({
    url: `/online/management/operator/app/${packageName}/check`,
    method: 'get'
  })
}

export function changeInstallationMode(appFileId) {
  return request({
    url: `/online/management/operator/app/${appFileId}/change/installation-mode`,
    method: 'post'
  })
}

// 添加灰度终端（按文件导入）
export function addCanaryDeviceByFile(params, data) {
  return request({
    url: `/online/management/operator/app/grayscales/${params.appFileId}/bind/import-devices`,
    method: 'post',
    data,
    timeout: 1000 * 60 * 10
  })
}

export function changeUninstallable(appFileId) {
  return request({
    url: `/online/management/operator/app/${appFileId}/change/uninstallable`,
    method: 'post'
  })
}

// 临时灰度设备
export function addCanaryDeviceTemp(data) {
  return request({
    url: `/online/management/operator/app/grayscales/temporary/save`,
    method: 'post',
    data
  })
}

export function getCanaryDeviceTempPage(params) {
  return request({
    url: '/online/management/operator/app/grayscales/temporary',
    method: 'get',
    params
  })
}

export function deleteCanaryDeviceTemp(params) {
  return request({
    url: `/online/management/operator/app/grayscales/temporary/${params.batchNo}/${params.devSn}/delete`,
    method: 'post'
  })
}

export function checkCanaryDeviceTemp(params) {
  return request({
    url: `/online/management/operator/app/grayscales/temporary/${params.batchNo}/${params.devSn}`,
    method: 'get'
  })
}

// 添加临时灰度终端（按文件导入）
export function addCanaryDeviceTempByFile(params, data) {
  return request({
    url: `/online/management/operator/app/grayscales/temporary/import-devices?batchNo=${params.batchNo}&appFileId=${params.appFileId}`,
    method: 'post',
    data,
    timeout: 1000 * 60 * 10
  })
}

export function queryDeployResult(batchNo, params) {
  return request({
    url: `/online/management/operator/app/grayscales/deploy-result/${batchNo}`,
    method: 'GET',
    params
  })
}

// 添加灰度终端
export function addCanaryDeviceByBatchNo(params) {
  return request({
    url: `/online/management/operator/app/grayscales/${params.appFileId}/${params.batchNo}/bind`,
    method: 'post'
  })
}

/**
 * @desc 获取任务列表
 */
export function getTaskList(params) {
  return request({
    url: `/online/management/task-center/list`,
    method: 'get',
    params
  })
}

/**
 * @desc 获取下载地址
 */
export function getDownloadUrl(downloadUrl) {
  return request({
    url: `/online/management/${downloadUrl}`,
    method: 'get'
  })
}

export function findTemplateByApp(params) {
  return request({
    url: `/online/management/operator/cmp/template`,
    method: 'get',
    params
  })
}

// -------------------------------------
/**
 * 查询系统应用列表
 * @param {*} params 
 * @returns 
 */
export function getSystemApp(params) {
  return request({
    url: `/online/management/statistic/system/application/list`,
    method: 'get',
    params
  })
}

/**
 * 查询系统应用版本
 * @param {*} params appPackageName
 * @returns 
 */
export function getSystemAppVersion(params) {
  return request({
    url: `/online/management/statistic/system/application/version/list`,
    method: 'get',
    params
  })
}

/**
 * 查看已安装该应用的设备列表（支持搜索，搜索条件：应用版本，SN，应用包名，运营商id等）
 * @param {*} params appPackageName(应用包名,非必传), appVersion(应用版本,非必传), charactorCode(ADMIN或者OPERATOR,必传), keyId(必传), devSn(非必传)
 * @returns 
 */
export function getDeviceAppInfo(params) {
  return request({
    url: `/online/management/statistic/device/info`,
    method: 'get',
    params
  })
}

/**
 * 导出已安装该应用的设备列表（支持搜索，搜索条件：应用版本，SN，应用包名，运营商id等）
 * @param {Object} params 输入参数： appPackageName(应用包名,非必传), appVersion(应用版本,非必传), charactorCode(ADMIN或者OPERATOR,必传), keyId(必传), devSn(非必传)
 * @returns 
 */
export function exportDeviceAppInfo(params) {
  return request({
    url: `/online/management/statistic/device/info/export`,
    method: 'get',
    params
  })
}
/**
 * 查看某个系统应用版本下载量 upgradeCount1：1天内升级，activeCount1：1天内活跃， gapNum:差距数
gapTotal: 差距总数，perishNum: 待消亡数
 * @param {Object} params 输入参数： appPackageName(应用包名,非必传), appVersion(应用版本,非必传)
*/
export function getAppInstallInfo(params) {
  return request({
    url: `/online/management/statistic/system/application/install/count`,
    method: 'get',
    params
  })
}
/**
 * 查看某个时间段内升级的数量
 * @param {Object} params
 * appPackageName(应用包名), appVersion(应用版本,非必传)
 * dataRange , dateUnit(String, "Day"或者"Month")，
 */
export function getUpgradeCount(params) {
  return request({
    url: `/online/management/statistic/system/application/upgrade/count`,
    method: 'get',
    params
  })
}
/**
 * 应用版本分布饼图
 * @param {Object} params
 * appPackageName(应用包名)
 */
export function getAppVersionPie(params) {
  return request({
    url: `/online/management/statistic/system/application/version/count`,
    method: 'get',
    params
  })
}

/**
 * --根据激活/升级导出该应用版本的设备列表
 * @param {object} params 输入参数： appPackageName(应用包名,必传), appVersion(应用版本,必传), exportType(active或者upgrade,必传), timeRange(必传,1,7,30,90)
 * @returns
 */
export function exportAppDeviceByType(params) {
  return request({
    url: `/online/management/statistic/system/application/device/export`,
    method: 'get',
    params
  })
}
/**
 *  --根据升级设备/滞留 设备 导出该应用版本的设备列表
 * @param {object} params 输入参数： appPackageName(应用包名,必传), appVersion(应用版本,必传), exportType(1:已经升级设备 2：未升级设备 3：滞留设备,必传),
 * @returns
 */
export function exportAppDeviceByStatus(params) {
  return request({
    url: `/online/management/statistic/system/application/device/info/export`,
    method: 'get',
    params
  })
}
